<?php
/*
Plugin Name: Nmedia Real Estate Plugin
Plugin URI: http://www.najeebmedia.com
Description: This Plugin is developed by NajeebMedia.com
Version: 1.1
Author: Najeeb Ahmad
Author URI: http://www.najeebmedia.com/
*/



/*ini_set('display_errors',1);
error_reporting(E_ALL);*/


add_action('admin_enqueue_scripts', 'load_plugin_scripts');

class nmRealEstate {
	
	var $estate_db_version = "1.0";
	
	/*
	data vars
	*/
	var $detail;
	var $price;
	var $postcode;
	var $rooms;
	var $area;
	var $sleeps;
	var $closest_tube;
	var $type;
	var $pets;
	var $smoking;
	var $deposit_info;
	var $internet;
	var $wifi;
	var $kitchen;
	var $garden;
	var $disabled_access;
	var $cable;
	var $tv;
	var $BBQ;
	//user personal info
	var $firstname;
	var $surname;
	var $telephone;
	var $email1;
	var $email2;
	var $images;			//it will be serialized data
	var $status = 'Pending';
	
	
	
	/**
	* constructor
	*/	 
	function nmRealEstate() {
		
		//$this -> loadJS();
	}
	
	
	
	
	public function estate_install() {
   global $wpdb;
   global $estate_db_version;

   $table_name = $wpdb->prefix . "propertylisting";
      
	$sql = "CREATE TABLE `$table_name` 
			(`property_ID` INT(8) NULL AUTO_INCREMENT PRIMARY KEY, 
			`property_detail` MEDIUMTEXT NULL, 
			`property_price` FLOAT(10,2) NULL, 
			`property_postcode` VARCHAR(15) NULL, 
			`property_rooms` INT(5) NULL, 
			`property_area` VARCHAR(150) NULL, 
			`property_sleeps` INT(5) NULL, 
			`property_closest_tube` VARCHAR(50) NULL, 
			`property_type` VARCHAR(50) NULL, 
			`property_pets` VARCHAR(15) NULL, 
			`property_smoking` VARCHAR(15) NULL, 
			`property_deposit_info` MEDIUMTEXT NULL, 
			`property_internet` TINYINT NULL, 
			`property_wifi` TINYINT NULL, 
			`property_kitchen` TINYINT NULL, 
			`property_garden` TINYINT NULL, 
			`property_disabled_access` TINYINT NULL, 
			`property_cable` TINYINT NULL, 
			`property_tv` TINYINT NULL, 
			`property_BBQ` TINYINT NULL, 
			`property_firstname` VARCHAR(150) NULL,
			`property_surname` VARCHAR(150) NULL, 
			`property_telephone` VARCHAR(150) NULL, 
			`property_email1` VARCHAR(200) NULL, 
			`property_email2` VARCHAR(200) NULL, 
			`property_images` MEDIUMTEXT NULL, 
			`property_listed_date` DATETIME NULL, 
			`property_status` ENUM('Pending','Approved','Rejected',
			`property_fee_paid` DEFAULT 0 ));";

   require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
   dbDelta($sql);
 
   add_option("estate_db_version", $estate_db_version);
}

	function estate_install_data() {
	   global $wpdb;
	   $welcome_name = "Mr. Wordpress";
	   $welcome_text = "Congratulations, you just completed the installation!";
	
	   $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'text' => $welcome_text ) );
	}
	
	
		
	
  
  public function set_up_admin_page () {
	
	add_menu_page(	'Realtor', 
					'Realtor Plugin', 
					'manage_options', 
					'realtor-settings', 
					array('nmRealEstate', 'show_listings'),
					'',
					4);
	
	add_submenu_page( 'realtor-settings',
					  'Property Listings', 
					  'Property Listings', 
					  'manage_options', 
					  'property-listings', 
					  array('nmRealEstate', 'show_listings'));
  	
	}
	
	
	public function setting_main_page()
	{
		$file = dirname(__FILE__).'/options.php';
		include($file);
	}
	
	public function show_listings()
	{
		$file = dirname(__FILE__).'/listings.php';
		include($file);
	}
	
 
  /*function nm_load_pt_form()
  {
  	$api_dir = dirname(__FILE__).'/inc/PrayTime.php';
	require_once $api_dir;
	
	$file = dirname(__FILE__).'/front.php';
	include($file);
  }*/
  
  
  
	public function saveListing()
	{
		
		global $wpdb;
		$dt = array(	'property_detail'	=> $this -> detail,
						'property_price'	=> $this -> price,
						'property_postcode'	=> $this -> postcode,
						'property_rooms'	=> $this -> rooms,
						'property_area'		=> $this -> area,
						'property_sleeps'	=> $this -> sleeps,
						'property_closest_tube'	=> $this -> closest_tube,
						'property_type'		=> $this -> type,
						'property_pets'		=> $this -> pets,
						'property_smoking'	=> $this -> smoking,
						'property_deposit_info'	=> $this -> deposit_info,
						'property_internet'	=> $this -> internet,
						'property_wifi'		=> $this -> wifi,
						'property_kitchen'	=> $this -> kitchen,
						'property_garden'	=> $this -> garden,
						'property_disabled_access'	=> $this -> disabled_access,
						'property_cable'	=> $this -> cable,
						'property_tv'		=> $this -> tv,
						'property_BBQ'		=> $this -> BBQ,
						'property_firstname'	=> $this -> firstname,
						'property_surname'	=> $this -> surname,
						'property_telephone'	=> $this -> telephone,
						'property_email1'	=> $this -> email1,
						'property_email2'	=> $this -> email2,
						'property_images'	=> $this -> images,
						'property_status'	=> $this -> status,
						'property_listed_date'	=> current_time('mysql')
					 );
					 
					 
		//var_dump($dt);
		
		$wpdb -> insert($wpdb->prefix . "propertylisting",
						$dt		
						);
		
		if($wpdb->insert_id)
			return true;
		else
			return false;
						
		//$wpdb->print_error(); 
	}
	
	
	/*
	deleting listing
	*/
	public function deleteListing($listid)
	{
		global $wpdb;
		if(is_array($listid))
		{
			$ids = implode(',', $listid);
			
			$res = $wpdb->query(
					"
					DELETE FROM ".$wpdb->prefix . "propertylisting
					WHERE property_ID IN ($ids)"
				);	
		}
		else
		{
			$res = $wpdb->query(
						"
						DELETE FROM ".$wpdb->prefix . "propertylisting
						WHERE property_ID = $listid"
					);
		}
				
		return $res;
	}
	
	
	/*
	setting status Pending | Approved | Rejected
	*/
	function setStatus($status, $listid)
	{
		global $wpdb;
							
		$fv 	= array('property_status'	=> $status);
		$where	= array('property_ID'		=> $listid);
		
		
		if(
			$wpdb -> update($wpdb->prefix . "propertylisting",
							$fv,
						$where		
							))
		{
			return true;
		}else
		{

			return false;
		}
		
	}
	
	
	
	/*
	setting payment flag to 1
	*/
	function listingPaid($email)
	{
		global $wpdb;
							
		$fv 	= array('property_fee_paid'	=> 1);
		$where	= array('property_email1'	=> $email);
		
		
		if(
			$wpdb -> update($wpdb->prefix . "propertylisting",
							$fv,
							$where		
							))
		{
			return true;
		}else
		{

			return false;
		}
		
	}
	
	
  
  
}

add_action('admin_menu', array('nmRealEstate', 'set_up_admin_page'));

register_activation_hook(__FILE__, array('nmRealEstate','estate_install'));
register_activation_hook(__FILE__, array('nmRealEstate','estate_install_data'));



function load_plugin_scripts() {
	wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', plugins_url('js/jquery-1.4.4.min.js', __FILE__));
	wp_enqueue_script( 'jquery' );
	
	wp_register_script( 'fancybox_script1', plugins_url('js/fancybox/jquery.mousewheel-3.0.4.pack.js', __FILE__), 'jquery');
	 wp_enqueue_script('fancybox_script1');
	wp_register_script( 'fancybox_script2', plugins_url('js/fancybox/jquery.fancybox-1.3.4.pack.js', __FILE__), 'jquery');
	wp_enqueue_script('fancybox_script2');
	 
	 
	
     wp_register_script( 'estate_custom_script', plugins_url('js/estate_custom.js', __FILE__), 'jquery');
	 wp_enqueue_script('estate_custom_script');
	 
	 //fancy box
}    
 

//var_dump($nm_sticky);
//$e -> saveListing();



//add_action('wp_print_styles', 'nm_estate_style');

    /*
     * Enqueue style-file, if it exists.
     */

    function nm_estate_style() {
		wp_register_style('property_stylesheet', plugins_url('js/fancybox/jquery.fancybox-1.3.4.css', __FILE__));
        wp_enqueue_style( 'property_stylesheet');
        
    }


?>